var async = require('async');
var request = require('request');
var cheerio = require('cheerio');
var path = require('path');
var UUID = require('uuid');
var fs = require('fs');
var config = require('./config.js');
var selectors = config.selectors;
// var dir = config.start + "到" + config.end;
var dir = "test";
var j = 0;
var i = 0;
var errs = {
    addrs: [],
    imgs: [],
};
exports.start = function(urls) {
    console.log('start...')
    var that = this;
    that.gethrefs(urls, selectors)
}
exports.gethrefs = function(hrefs, selectors, _cb) {
    var that = this;
    i++;
    var depth = selectors.length;

    async.mapSeries(hrefs, function(href, cb) {

        request(href, { timeout: config.timeout }, function(error, response, body) {
            if (!error && response.statusCode == 200) {
                var $ = cheerio.load(body);

                var _hrefs = [];
                var links = $(selectors[0]);
                links.map(function(index, link) {
                    _hrefs.push(link.attribs.href)
                });
                if (depth > 1) {
                    that.gethrefs(_hrefs, selectors.slice(1), function() {
                        cb(null)
                    });
                } else {
                    that.getImages(_hrefs, function() {
                        i++;
                        cb(null);
                    });
                }
            } else {
                errs.addrs.push(href);
                cb(null);
            }
        });

    }, function() {
        if (_cb) {

            _cb.call(this, null);
        } else {
            console.log(errs);
            fs.writeFileSync('./error.json', JSON.stringify(errs), { flag: "w" });
            console.log('全部下载完成')
        }

    })
}
exports.getImages = function(hrefs, cb) {
    var current = 0;
    var total = 0;
    async.mapSeries(hrefs, function(href, _cb) {
        if (!fs.existsSync(path.resolve(__dirname, "download", dir))) {
            fs.mkdir(path.resolve(__dirname, "download", dir));
        }
        var filename = UUID.v1();
        request.get(href, { timeout: config.timeout }, function(error) {
                if (error) {
                    if (fs.existsSync(path.resolve(__dirname, 'download/', dir, filename + '.jpg'))) {
                        fs.unlinkSync(path.resolve(__dirname, 'download/', dir, filename + '.jpg'));
                    }
                    errs.imgs.push(href);
                }
                _cb(null);
            })
            .on('response', function(res) {
                total = res.headers['content-length'];
            })
            .on('data', function(data) {
                current += data.length;
                console.log((current / total).toFixed(4) * 100 + "%");

            })
            .pipe(fs.createWriteStream(path.resolve(__dirname, 'download', dir, filename + '.jpg')));
    }, function() {
        cb.call(this, null);
    });
}